#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Project Euler Solution 058

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
import math
from euler.numbers.primes import isprime, Primes

def get_answer():
    """Question:
    
    Starting with 1 and spiralling anticlockwise in the following way, a square 
    spiral with side length 7 is formed.

    37 36 35 34 33 32 31
    38 17 16 15 14 13 30
    39 18  5  4  3 12 29
    40 19  6  1  2 11 28
    41 20  7  8  9 10 27
    42 21 22 23 24 25 26
    43 44 45 46 47 48 49
    
    It is interesting to note that the odd squares lie along the bottom right 
    diagonal, but what is more interesting is that 8 out of the 13 numbers 
    lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%.
    
    If one complete new layer is wrapped around the spiral above, a square 
    spiral with side length 9 will be formed. If this process is continued, 
    what is the side length of the square spiral for which the ratio of primes 
    along both diagonals first falls below 10%?
    """
    
    #Cache for prime numbers.    
    prime_cache = Primes()
    
    #Initialise known values.
    
    #The percentage of primes within the spiral
    prime_percentage = 62
    
    #The current layer of the spiral, with the center being layer 1. 
    layer = 4
    
    #The total number of odd numbers within the spiral.
    total_odd_numbers = 13
    
    #The total number of primes within the spiral.
    total_primes = 8
    
    #The width of the spiral.
    width = 7  
    
    #Keep adding layers to the spiral until a prime percentage
    #below 10 is reached.
    while prime_percentage >= 10:
        
        #Update variables.
        layer += 1
        width += 2
        
        total_odd_numbers += 4
        
        area = width ** 2
        
        #Count the number of primes in the corners of the current layer.
        if(isprime(area, prime_cache)):
            total_primes += 1 
            
        if(isprime(area - (width - 1), prime_cache)):
            total_primes += 1 
                    
        if(isprime(area - (width - 1) * 2, prime_cache)):
            total_primes += 1 
                    
        if(isprime(area - (width - 1) * 3, prime_cache)):
            total_primes += 1 
        
        #Update the percentage of primes.
        prime_percentage = math.floor(
                                      (float(total_primes) / total_odd_numbers) 
                                        * 100
                                    )
    
    #Return result.
    return width
    
if __name__ == "__main__":
    cProfile.run("print(get_answer())")
